#include "packagerui.h"

#define BASICLENHTH 1
#define MUTIPLE  7
PackagerUI::PackagerUI(QWidget *parent) : QWidget(parent)
{
    this->setWindowIcon(QIcon(":/QSS/image/Window_Icon.png"));
    this->setObjectName(tr("PackagerUI"));
    initWidget();
   // this->show();
}

//构建所有界面元素
void PackagerUI::initWidget(){

    //总布局
    QGridLayout *MainLayout=new QGridLayout();
    MainLayout->setVerticalSpacing(15);
    MainLayout->setMargin(15);
    this->setLayout(MainLayout);

    MainStackWidget = new QStackedWidget();   
    previousButton = new QPushButton(tr("Previous"));
    previousButton->setObjectName(tr("previousButton"));
    nextButton = new QPushButton(tr("Next"));
    nextButton->setObjectName("nextButton");
    GeneratePackage =new QPushButton(tr("Pack"));
    GeneratePackage->setObjectName(tr("PackButton"));

    //IconHelper::Instance()->SetIcon(*GeneratePackage,QChar(0xf177),12);



    QToolButton *PreviewButton = new QToolButton();
    PreviewButton->setObjectName(tr("Preview"));
    PreviewButton->setToolTip(tr("Preview"));

    //导航按钮
    QVBoxLayout *ButtonsGroupLayout = new QVBoxLayout();
    mButtonsGroup = new QButtonGroup();

    QToolButton *EntranceButton = new QToolButton();
    EntranceButton->setObjectName(tr("Entrance"));
    EntranceButton->setText(tr("Entrance"));
    EntranceButton->setCheckable(true);
    EntranceButton->setChecked(true);

    QToolButton *LibraryButton = new QToolButton();
    LibraryButton->setObjectName(tr("Library"));
    LibraryButton->setText(tr("Library"));
    LibraryButton->setCheckable(true);
    LibraryButton->setChecked(false);

    QToolButton *HardwareButton = new QToolButton();
    HardwareButton->setObjectName(tr("Hardware"));
    HardwareButton->setText(tr("Hardware"));
    HardwareButton->setCheckable(true);

    QToolButton *SoftwareButton = new QToolButton();
    SoftwareButton->setObjectName(tr("Software"));
    SoftwareButton->setText(tr("Software"));
    SoftwareButton->setCheckable(true);

    QToolButton *TestifyButton = new QToolButton();
    TestifyButton->setObjectName(tr("Testify"));
    TestifyButton->setText(tr("Testify"));
    TestifyButton->setCheckable(true);

    QToolButton *SupportiveButton = new QToolButton();
    SupportiveButton->setObjectName("Supportive");
    SupportiveButton->setText(tr("Supportive"));
    SupportiveButton->setCheckable(true);

    QToolButton *LicenseButton = new QToolButton();
    LicenseButton->setObjectName(tr("License"));
    LicenseButton->setText(tr("License"));
    LicenseButton->setCheckable(true);


    mButtonsGroup->addButton(EntranceButton,0);
    mButtonsGroup->addButton(LibraryButton,1);
    mButtonsGroup->addButton(HardwareButton,2);
    mButtonsGroup->addButton(SoftwareButton,3);
    mButtonsGroup->addButton(TestifyButton,4);
    mButtonsGroup->addButton(SupportiveButton,5);
    mButtonsGroup->addButton(LicenseButton,6);

    mButtonsGroup->setExclusive(true);

    ButtonsGroupLayout->addWidget(EntranceButton);
    ButtonsGroupLayout->addWidget(LibraryButton);
    ButtonsGroupLayout->addWidget(HardwareButton);
    ButtonsGroupLayout->addWidget(SoftwareButton);
    ButtonsGroupLayout->addWidget(TestifyButton);
    ButtonsGroupLayout->addWidget(SupportiveButton);
    ButtonsGroupLayout->addWidget(LicenseButton);

    MainLayout->addLayout(ButtonsGroupLayout,0,0,MUTIPLE,1,Qt::AlignTop);

    MainLayout->addWidget(MainStackWidget,0,1,MUTIPLE,MUTIPLE);
    MainLayout->addWidget(previousButton,MUTIPLE+BASICLENHTH,MUTIPLE-2,BASICLENHTH,BASICLENHTH);
    MainLayout->addWidget(nextButton,MUTIPLE+BASICLENHTH,MUTIPLE-1,BASICLENHTH,BASICLENHTH);
    MainLayout->addWidget(GeneratePackage,MUTIPLE+BASICLENHTH,MUTIPLE,BASICLENHTH,BASICLENHTH);


    //--Model Entrance
    QGroupBox *ModelEntrance=new QGroupBox(tr("Model Entrance"));
    MainStackWidget->addWidget(ModelEntrance);

    QGridLayout *ModelEntranceLayout=new QGridLayout();
    ModelEntranceLayout->setMargin(20);
    ModelEntranceLayout->setSpacing(5);
    ModelEntrance->setLayout(ModelEntranceLayout);

    //----model Type
    ChooseModel=new QComboBox();
    ChooseModel->addItem(tr("DLL"));
    ChooseModel->addItem(tr("OMS"));
    ChooseModel->addItem(tr("EXE"));

    ModelEntranceLayout->addWidget(new QLabel(tr("Model Type")),0,0,1,1);
    ModelEntranceLayout->addWidget(ChooseModel,0,1,1,MUTIPLE-BASICLENHTH);

    //----openProject Button
    openOMS=new QPushButton(tr("Add OMS"));
    openOMS->setEnabled(false);
    openOMS->setVisible(false);
    ModelEntranceLayout->addWidget(openOMS,1,1,1,2);


    //----openProject
    ProjectPath=new QLineEdit();
    ProjectPath->setEnabled(false);
    ProjectPath->setVisible(false);
    projectPathLabel = new QLabel(tr("OMS Project Path："));
    projectPathLabel->setVisible(false);
    ProjectRelativePathLabel = new QLabel(tr("Relative Path:"));
    ProjectRelativePathLabel->setVisible(false);
    ProjectRelativePath = new QLineEdit();
    ProjectRelativePath->setVisible(false);
    ProjectRelativePath->setFixedWidth(120);
    ModelEntranceLayout->addWidget(projectPathLabel,2,0,1,1);
    ModelEntranceLayout->addWidget(ProjectPath,2,1,1,MUTIPLE-BASICLENHTH-2);
    ModelEntranceLayout->addWidget(ProjectRelativePathLabel,2,MUTIPLE-BASICLENHTH-1,1,1);
    ModelEntranceLayout->addWidget(ProjectRelativePath,2,MUTIPLE-BASICLENHTH,1,1);

    //----ModelInputParameter
    QLabel *ModelInputParameterLabel = new QLabel(tr("Input Parameter "));
    ModelInputParameter = new QLineEdit(tr(""));
    ModelEntranceLayout->addWidget(ModelInputParameterLabel,3,0,1,1);
    ModelEntranceLayout->addWidget(ModelInputParameter,3,1,1,MUTIPLE-BASICLENHTH);

    //----set conversion methods
    setConversionMethods=new QPushButton(tr("CMethods"));
    ModelEntranceLayout->addWidget(setConversionMethods,4,1,1,2);


    //--Model Library
    QGroupBox *ModelLibrary=new QGroupBox(tr("Model Library"));
    MainStackWidget->addWidget(ModelLibrary);

    QGridLayout *ModelLibraryLayout=new QGridLayout();
    ModelLibrary->setLayout(ModelLibraryLayout);
    //----ModelLib Name

    //----添加依赖库按钮
    ChooseLibs=new QPushButton(tr("Add"));
    LibItemsRemove=new QPushButton(tr("Remove"));

    //添加libs表
    LibNamesTable=new QTableWidget();
    QStringList headerLabels;
    headerLabels <<"LibName"<<"Path";
    initTableStyle(headerLabels,LibNamesTable);

    ModelLibraryLayout->addWidget(ChooseLibs,0,0,BASICLENHTH,BASICLENHTH);
    ModelLibraryLayout->addWidget(LibItemsRemove,0,1,BASICLENHTH,BASICLENHTH);
    ModelLibraryLayout->addWidget(LibNamesTable,1,0,MUTIPLE,MUTIPLE);

    //--Hardware Condition
    QGroupBox *HardwareCondition =new QGroupBox(tr("Hardware Condition"));
    MainStackWidget->addWidget(HardwareCondition);

    QGridLayout *HardwareConditionLayout=new QGridLayout();
    HardwareCondition->setLayout(HardwareConditionLayout);    
    AddHardwareConItem=new QPushButton(tr("Add"));
    HardConRemove=new QPushButton(tr("Remove"));


    HardConTable=new QTableWidget();
    QStringList HardConTableLabels;
    HardConTableLabels << "Key" <<"Value";
    initTableStyle(HardConTableLabels,HardConTable);

    HardwareConditionLayout->addWidget(AddHardwareConItem,0,0,BASICLENHTH,BASICLENHTH);
    HardwareConditionLayout->addWidget(HardConRemove,0,1,BASICLENHTH,BASICLENHTH);
    HardwareConditionLayout->addWidget(HardConTable,1,0,MUTIPLE,MUTIPLE);

    //--Software Enviroment
    QGroupBox *SoftwareEnviroment =new QGroupBox(tr("Software Environment"));
    MainStackWidget->addWidget(SoftwareEnviroment);

    QGridLayout *SoftwareEnviromentLayout=new QGridLayout();
    SoftwareEnviroment->setLayout(SoftwareEnviromentLayout);

    AddSoftEnvirItem=new QPushButton(tr("Add"));
    SoftEnvirRemove=new QPushButton(tr("Remove"));

    SoftEnvirTable=new QTableWidget();
    QStringList SoftwareEnviromentLabels;
    SoftwareEnviromentLabels << "Key" <<"Value";
    initTableStyle(SoftwareEnviromentLabels,SoftEnvirTable);

    SoftwareEnviromentLayout->addWidget(AddSoftEnvirItem,0,0,1,1);
    SoftwareEnviromentLayout->addWidget(SoftEnvirRemove,0,1,BASICLENHTH,BASICLENHTH);
    SoftwareEnviromentLayout->addWidget(SoftEnvirTable,1,0,MUTIPLE,MUTIPLE);

    //--Testify Data
    QGroupBox *TestifyData =new QGroupBox(tr("Testify Data"));
    MainStackWidget->addWidget(TestifyData);

    QGridLayout *TestifyDataLayout=new QGridLayout();
    TestifyData->setLayout(TestifyDataLayout);

    AddTestDataItem=new QPushButton(tr("Add"));
    TestDataRemove=new QPushButton(tr("Remove"));

    TestDataList   =new QTableWidget();
    QStringList TestifyDataLabels;
    TestifyDataLabels << "dataName" <<"Path";
    initTableStyle(TestifyDataLabels,TestDataList);

    TestifyDataLayout->addWidget(AddTestDataItem,0,0,1,1);
    TestifyDataLayout->addWidget(TestDataRemove,0,1,BASICLENHTH,BASICLENHTH);
    TestifyDataLayout->addWidget(TestDataList,1,0,MUTIPLE,MUTIPLE);

    //--Supportive Resources
    QGroupBox *SupportiveResources =new QGroupBox(tr("Supportive Resources"));
    MainStackWidget->addWidget(SupportiveResources);

    QGridLayout *SupportiveResourcesLayout=new QGridLayout();
    SupportiveResources->setLayout(SupportiveResourcesLayout);


    AddSuportResItem=new QPushButton(tr("Add"));

    SuportResRemove=new QPushButton(tr("Remove"));

    SuportResList   =new QTableWidget();
    QStringList SupportiveResourcesLabels;
    SupportiveResourcesLabels << "dataName" <<"Path";
    initTableStyle(SupportiveResourcesLabels,SuportResList);

    SupportiveResourcesLayout->addWidget(AddSuportResItem,0,0,1,1);
    SupportiveResourcesLayout->addWidget(SuportResRemove,0,1,BASICLENHTH,BASICLENHTH);
    SupportiveResourcesLayout->addWidget(SuportResList,1,0,MUTIPLE,MUTIPLE);

    //--License
    QGroupBox *License = new QGroupBox(tr("License"));
    MainStackWidget->addWidget(License);

    QGridLayout *LicenseLayout=new QGridLayout();
    License->setLayout(LicenseLayout);

    ChooseLicense=new QComboBox();
    QStringList licenseList;
    licenseList<<"Apache Licence 2.0"
               <<"GPL"
               <<"BSD"
               <<"MIT"
               <<"MPL"
               <<"LGPL"
               <<"CustomLicense";

    ChooseLicense->addItems(licenseList);
    LicenseTextArea   = new QTextEdit();
    //GenerateScript  =new QPushButton(tr("Apache Testify Script"));

    LicenseLayout->addWidget(new QLabel(tr("Choose license")),0,0,1,1);
    LicenseLayout->addWidget(ChooseLicense,0,1,1,2);
    LicenseLayout->addWidget(LicenseTextArea,1,0,4,3);
    //LicenseLayout->addWidget(GenerateScript,6,0,1,1);
    //LicenseLayout->addWidget(GeneratePackage,6,2,1,1);


    connect(this->previousButton,SIGNAL(clicked(bool)),this,SLOT(previousButton_clicked()));
    connect(this->nextButton,SIGNAL(clicked(bool)),this,SLOT(nextButton_clicked()));
    connect(this,SIGNAL(changeWidget(int)),this->MainStackWidget,SLOT(setCurrentIndex(int)));
    connect(mButtonsGroup,SIGNAL(buttonClicked(int)),this,SLOT(on_ButtonGroup_click(int)));
    connect(PreviewButton,SIGNAL(clicked(bool)),this,SLOT(on_PreviewButton_click()));



}

void PackagerUI::previousButton_clicked(){
    int index=this->MainStackWidget->currentIndex();
    if(index>0){
        emit sendStateInfo(tr(""));
       emit this->changeWidget(index-1);
        changeButtonGroup(index-1);
    }else{
        emit sendStateInfo(tr("Is the first step."));
    }
}

void PackagerUI::nextButton_clicked(){
    int index=this->MainStackWidget->currentIndex();
    if(index<6){
        emit sendStateInfo(tr(""));
        emit this->changeWidget(index+1);
        changeButtonGroup(index+1);

    }else{
         emit sendStateInfo(tr("Is the last step."));
    }
}

//初始化表格
void PackagerUI::initTableStyle(QStringList headerLabels,QTableWidget *dataTable){
    dataTable->setColumnCount(2);
    dataTable->setHorizontalHeaderLabels(headerLabels);
    dataTable->setEditTriggers(QAbstractItemView::DoubleClicked);   //表格双击编辑
    dataTable->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    dataTable->setAlternatingRowColors(false);
    dataTable->setDragEnabled(false);

    //设置表头
    QHeaderView *headerView=new QHeaderView(Qt::Horizontal);
    headerView->setSectionResizeMode(QHeaderView::Stretch);
    headerView->setMinimumSectionSize(100);
    headerView->setStretchLastSection(true);
    dataTable->setHorizontalHeader(headerView);
    dataTable->verticalHeader()->setHidden(true);

}

void PackagerUI::on_ButtonGroup_click(int mIndex){
    emit  this->changeWidget(mIndex);
}

void PackagerUI::changeButtonGroup(int index){
    if(index>=0&&index<=6){
        this->mButtonsGroup->button(index)->setChecked(true);
    }
}
void PackagerUI::on_PreviewButton_click(){
    emit preview_clicked(0);
}
